#include <iostream>
#include <stdio.h>
#include "SENTRYTHREAD.h"
#include "additions.h"

using namespace std;
using namespace cv;

extern rm::Setter rm_setter;
extern Serial serial;
extern WrapperHead *video;

static uint8_t gEnemyColor;
void debugSetting(const rm::Setter& setter,WrapperHead *&video);
bool camera_init(rm::Setter &setter,WrapperHead *&video,Mat &src);

int main(int argv,char **argc) {
    if(argv == 1){
        Mat src;
        debugSetting(rm_setter,video);

        while (true) {
            bool _ok = camera_init(rm_setter,video,src);

            do {
                sentryFight(rm_setter,video,serial,gEnemyColor);
//多线程
            } while (_ok);

            delete video;
            video = nullptr;
#ifdef SAVE_INFO
            cout << "Program fails. Restarting" << endl;
#endif // SAVE_INFO
            cerr << "Program fails. Restarting" << endl;
        }

    }else if(argv == 2 && argc[1][0] == 'c'){         //Run in a special mode -- Clibration functionality
//        Calibrator calibrator(setter.Calibration.data(),setter.CornersMatrix,setter.Rectangle);
//        calibrator.save_calibrate_result();
    }else if(argv == 2 && argc[1][0] == 't'){         //Run in a special mode -- Test
        std::cout << "Alliance Nanjing University of Science & Technology Nanjing Jiangsu China" << std::endl;
    }else{
        std::cout << "Please Select one correct parameter!" << std::endl;
    }
    return 0;
}

extern String Num_Path1,Num_Path2,Wrong_Path,Show_Path,Miss_Path;
extern int gFromCamera;

void debugSetting(const rm::Setter& setter,WrapperHead *&video){
    gFromCamera=DEBUG_WITH_CAMERA;
#ifdef DEBUG_MODE
#if DEBUG_WITH_CAMERA==0
    char color;
    LOGS("PUT in ENEMY COLOR: r/b");
    cin >> color;
    if (color == 'b'||'B') gEnemyColor = ENEMY_BLUE;//默认颜色定义为红色了

    std::string video_name;
    LOGS( "PUT in video name.type: ");
    cin >> video_name;
    std::string video_path = PROJECT_DIR"/video/" + video_name;
    std::cout<<video_path<<std::endl;
    video = new VideoWrapper(video_path);
#endif //DEBUG_WITH_CAMERA
#else
    gFromCamera=1;
//    setter.show_src_cnt=0;
//    setter.save_show_picture=0;
//    setter.show_armor=1;
//    setter.show_CSYS=0;
//    setter.show_track_box=1;
//    setter.show_armor=1;

#endif//DEBUG_MODE

    serial.setFCNTL();

    if(setter.save_num_picture){
        char *p_cMkdir1,*p_cMkdir2;
        makeDir(SAVE_PICTURE_PATH"num/no-2/",p_cMkdir1);
        makeDir(SAVE_PICTURE_PATH"num/2/",p_cMkdir2);
        String s1(p_cMkdir1);
        String s2(p_cMkdir2);
        std::cout<<Num_Path1<<endl;
        Num_Path1=s1;
        Num_Path2=s2;

    }
    if(setter.save_wrong_picture){
        char *p_cMkdir1;
        makeDir(SAVE_PICTURE_PATH"wrong/",p_cMkdir1);
        String s1(p_cMkdir1);
        Wrong_Path=s1;
    }
    if(setter.save_show_picture){
        char *p_cMkdir1;
        makeDir(SAVE_PICTURE_PATH"show/",p_cMkdir1);
        String s1(p_cMkdir1);
        Show_Path=s1;
    }
    if(setter.save_miss_picture){
        char *p_cMkdir1;
        makeDir(SAVE_PICTURE_PATH"miss/",p_cMkdir1);
        String s1(p_cMkdir1);
        Miss_Path=s1;
    }

};

bool camera_init(rm::Setter &setter,WrapperHead *&video,Mat &src){
    if (gFromCamera==1) {
        video = new CameraWrapper(setter.armor_camera_exposure,setter.armor_camera_gain,0);
    } else {

    }
    if (video->init()) {
        LOGS("video_source initialization successfully.");
    }
    else {
        LOGE("video_source unavailable!");
        return false;
    }
    for (int i = 0; i < 5; i++) {
        if (video) {
            video->read(src);
        }
    }
    return true;
}
